package cn.hchaojie.camp.javafx.controls;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class DemoListView extends Application {

	@Override
	public void start(Stage stage) throws Exception {
		Scene scene = new Scene(createContents(), 400, 250, Color.WHITE);

		stage.setScene(scene);
		stage.show();
	}

	// 在这里创建页面布局
	private Parent createContents() {
		GridPane gridpane = new GridPane();
		gridpane.setPadding(new Insets(5));
		gridpane.setHgap(10);
		gridpane.setVgap(10);
		ColumnConstraints column1 = new ColumnConstraints(150, 150, Double.MAX_VALUE);
		ColumnConstraints column2 = new ColumnConstraints(50);
		ColumnConstraints column3 = new ColumnConstraints(150, 150, Double.MAX_VALUE);

		column1.setHgrow(Priority.ALWAYS);
		column3.setHgrow(Priority.ALWAYS);
		gridpane.getColumnConstraints().addAll(column1, column2, column3);

		// Candidates label
		Label candidatesLbl = new Label("Candidates");
		GridPane.setHalignment(candidatesLbl, HPos.CENTER);
		gridpane.add(candidatesLbl, 0, 0);

		// Heroes label
		Label heroesLbl = new Label("Heroes");
		gridpane.add(heroesLbl, 2, 0);
		GridPane.setHalignment(heroesLbl, HPos.CENTER);

		// Candidates
		final ObservableList<String> candidates = FXCollections.observableArrayList("Superman", "Spiderman",
				"Wolverine", "Police", "Fire Rescue", "Soldiers", "Dad & Mom", "Doctor", "Politician", "Pastor",
				"Teacher");
		final ListView<String> candidatesListView = new ListView<>(candidates);
		candidatesListView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
		gridpane.add(candidatesListView, 0, 1);

		// heroes
		final ObservableList<String> heroes = FXCollections.observableArrayList();
		final ListView<String> heroListView = new ListView<>(heroes);
		gridpane.add(heroListView, 2, 1);

		// select heroes
		Button sendRightButton = new Button(" > ");
		sendRightButton.setOnAction((ActionEvent event) -> {
			String[] selected = candidatesListView
					.getSelectionModel()
					.getSelectedItems()
					.toArray(new String[0]);
			if (selected != null) {
				candidates.removeAll(selected);
				heroes.addAll(selected);
			}
		});

		// deselect heroes
		Button sendLeftButton = new Button(" < ");
		sendLeftButton.setOnAction((ActionEvent event) -> {
			String notHero = heroListView.getSelectionModel().getSelectedItem();
			if (notHero != null) {
				heroes.remove(notHero);
				candidates.add(notHero);
			}
		});
		VBox vbox = new VBox(5);
		vbox.getChildren().addAll(sendRightButton, sendLeftButton);

		gridpane.add(vbox, 1, 1);

		return gridpane;
	}

	public static void main(String[] args) {
		launch(args);
	}
}
